// pages/login/login.js
var app = getApp();
var ddapi = require('../../utils/md5.js');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		verifyCodeTime: '获取验证码',
    avatarUrl: '',
    nickName: ''
	},
	mobileInputEvent: function (e) {
		this.setData({
			mobile: e.detail.value
		})
	},
	bindSave: function (e) {
		var that = this;
		var mobile = e.detail.value.mobile;
		var code = e.detail.value.code;
    var avatarUrl = that.data.avatarUrl;
    var nickName = that.replace(that.data.nickName);
		if (mobile == "") {
			wx.showModal({
				title: '提示',
				content: '请填写手机号码',
				showCancel: false
			})
			return
		}
		if (code == "") {
			wx.showModal({
				title: '提示',
				content: '请填写验证码',
				showCancel: false
			})
			return
		}

    wx.showToast({
      title: '登录中',
      icon: 'loading'
    })
    var sign = ddapi.md5(avatarUrl + code + nickName + mobile + 'User.SendLoginWx');
		console.log(sign)
		wx.request({
      url: app.globalData.serviceUrl + 'User.SendLoginWx',
			header: {
				'Content-Type': 'json'
			},
			data: {
				sign: sign,
				reg_tel: mobile,
				captcha:code,
        avatar: avatarUrl,
        nickname: nickName
			},
			success: function (res) {
				if (res.data.data.code == 0) {
          var info = res.data.data.info;
          if (info.user_id != undefined) {
            wx.setStorageSync('openId', info.open_id);
            wx.setStorageSync('userId', info.user_id);
            wx.setStorageSync('nickname', info.nickname);
            wx.setStorageSync('avatar', info.avatar);
            wx.setStorageSync('userTel', mobile);
            app.globalData.openId = info.open_id;
            app.globalData.userId = info.user_id;
            app.globalData.mobile = mobile;
            app.globalData.userTel = wx.getStorageSync('userTel');
            app.globalData.nickname = wx.getStorageSync('nickname');
            app.globalData.avatar = wx.getStorageSync('avatar');

            that.CheckLand(info, mobile);
          } else {
            wx.hideToast()
            wx.showToast({
              title: '登陆失败',
              icon: 'success',
              duration: 2000
            })
          }
				} else {
          wx.showToast({
            title: '登陆失败',
            icon: 'success',
            duration: 2000
          })
				}
			},
			complete: function(res) {
				console.log(res);
			},
		})
	},

  //过滤emoji表情符号
  replace: function (nickName) {
    return nickName.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, "");
  },

	verifyCodeEvent: function (e) {
		if (this.data.buttonDisable) return false;
		var that = this;
		var c = 60;
		var intervalId = setInterval(function () {
			c = c - 1;
			that.setData({
				verifyCodeTime: c + 's后重发',
				buttonDisable: true
			})
			if (c == 0) {
				clearInterval(intervalId);
				that.setData({
					verifyCodeTime: '获取验证码',
					buttonDisable: false
				})
			}
		}, 1000)
		var mobile = this.data.mobile;
		var regMobile = /^1\d{10}$/;
		if (!regMobile.test(mobile)) {
			wx.showToast({
				title: '手机号有误！',
				image: '/images/popup-close.png'
			})
			return false;
		}
		var sign = ddapi.md5(mobile+'User.SendCaptcha');
		console.log(sign)
		wx.request({
			url: app.globalData.serviceUrl + 'User.SendCaptcha',
			header: {
				'Content-Type': 'json'
			},
			data: {
				sign: sign,
				reg_tel: mobile
			},
      success: function (res) {
				if(res.data.data.code==0){
          that.setData({
            avatarUrl: e.detail.userInfo.avatarUrl,
            nickName: e.detail.userInfo.nickName,
          });
					wx.showToast({
						title: '发送成功'
					})
				}else{
					wx.showToast({
						title: '发送失败',
						image: '/images/popup-close.png'
					})
				}
			},
			complete: function(res) {
				console.log(res);
			},
		})
	},

  CheckLand: function (info, mobile){
    var that = this;
    var service = 'User.CheckLand';
    var open_id = info.open_id;
    var user_id = info.user_id;
    var sign = ddapi.md5(open_id + service + user_id);
    wx.request({
      url: app.globalData.serviceUrl + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
      },
      success: (res) => {
        var result = res.data.data;
        if (result.code == 0) {
          if (result.info == 0) {
            that.editLand(info, mobile);
          }else{
            wx.switchTab({
              url: '/pages/ucenter/index/index',
            });
          }
        }
      }
    })
  },
  

  editLand: function (info, mobile) {

    var service = 'User.EditLand';
    var open_id = info.open_id;
    var user_id = info.user_id;
    var pid = 1;
    var sign = ddapi.md5(open_id + pid + service + user_id);
    wx.request({
      url: app.globalData.serviceUrl + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
        pid: pid,
      },
      success: (res) => {
        wx.hideToast()
        var result = res.data.data;
        if (result.code == 0) {
          wx.switchTab({
            url: '/pages/ucenter/index/index',
          });
        }else{
          wx.showToast({
            title: '修改失败',
            icon: 'success',
            duration: 2000
          })
        }
      }
    })
  }
  
})